const https = require('https');

const userList = [
    {
        names: ['章桐', 'TongZ'],
        phone: 18958013866,
        mail: 'zhangt@weipaitang.com',
    },
    {
        names: ['林文豪', 'lwhao'],
        phone: 13173619622,
        mail: 'linwh@weipaitang.com',
    },
    {
        names: ['王勇', 'wangy'],
        phone: 18668065162,
        mail: 'wangy@weipaitang.com',
    },
    {
        names: ['李俊', 'lijun'],
        phone: 15949565123,
        mail: 'lij@weipaitang.com',
    },
    {
        names: ['黄寿辉', 'huangshouhui'],
        phone: 15258880636,
        mail: 'hsh@weipaitang.com',
    },
    {
        names: ['胡世豪'],
        phone: 15558066513,
        mail: 'hush@weipaitang.com',
    },
    {
        names: ['徐泽文'],
        phone: 13567154774,
        mail: 'xuzw@weipaitang.com',
    },
    {
        names: ['zhuangjunyu'],
        phone: 13958002089,
        mail: 'zhuangjy@weipaitang.com',
    },
    {
        names: ['肖志华', 'xiaozhihua'],
        phone: 18750951774,
        mail: 'xiaozh@weipaitang.com',
    },
    {
        names: ['黄少良', 'stanley'],
        phone: 13777382629,
        mail: 'huangsl@weipaitang.com',
    },
    {
        names: ['周栗刚'],
        phone: 15868819694,
        mail: 'zhoulg@weipaitang.com',
    },
    {
        names: ['葛圣明', 'geshengming'],
        phone: 15558071220,
        mail: 'gesm@weipaitang.com',
    },
    {
        names: ['赵锌泽'],
        phone: 15101668168,
        mail: 'zhaoxz@weipaitang.com',
    },
    {
        names: ['梁青'],
        phone: 15968846096,
        mail: 'liangq@weipaitang.com',
    },
    {
        names: ['王瑞江'],
        phone: 15869120517,
        mail: 'wangrj@weipaitang.com',
    },
    {
        names: ['茅日盛'],
        phone: 13758235326,
        mail: 'maors@weipaitang.com',
    },
    {
        names: ['高瞻'],
        phone: 18868810643,
        mail: 'gaoz@weipaitang.com',
    },
    {
        names: ['王新宇'],
        phone: 18210711176,
        mail: 'wangxy@weipaitang.com',
    },
    {
        names: ['鲁延涛'],
        phone: 16729209555,
        mail: 'luyt@weipaitang.com',
    },
];

function post() {
    const [, , email, jobId, branch, status = ''] = process.argv;
    const { phone = '' } = userList.find((i) => i.mail === email) || {};

    const getContent = () => {
        let content = '';

        switch (Number(status)) {
            case 0: // fail
                content = `${branch} ❌❌发布出错❌❌:\n请去 https://gitlab.weipaitang.com/f2e/webApp/pipelines/${jobId} 查看`;
                break;
            case 1:
                content = `${branch} 🚀🚀发布成功🚀🚀:\n请去 https://gitlab.weipaitang.com/f2e/webApp/pipelines/${jobId} 查看`;
                break;
            case 2:
                content = `${branch} 🚧🚧开始发布🚧🚧:\n可以去 https://gitlab.weipaitang.com/f2e/webApp/pipelines/${jobId} 查看详细情况`;
                break;
            default:
                break;
        }

        return content;
    };

    const data = JSON.stringify({
        msgtype: 'text',
        text: {
            content: getContent(),
            mentioned_mobile_list: [`${phone}`],
        },
    });

    const req = https.request({
        hostname: 'qyapi.weixin.qq.com',
        port: 443,
        path: '/cgi-bin/webhook/send?key=38978fcf-fcb0-4efd-a4cd-b5c7f57e1c9c',
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
        },
    });

    req.write(data);
    req.end();
}

post();
